<?php

namespace plugins\monitor\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\address\model\AddressModel;
use plugins\device\model\DeviceModel;
use think\Db;

/**
 * 实时监测
 */
class AdminIndexController extends PluginAdminBaseController
{

    /**
     * 实时监测首页
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 获取地区-设备列表
     */
    public function getAddressDeviceList(){
        $param = $this->request->param();
        $addressModel = new AddressModel();
        $da = $addressModel->field('cri_id,cri_code,cri_name,cri_lng,cri_lat,cri_superior_code')->order('cri_code asc')->select()->each(function ($item){
            $item['label'] = $item['cri_name'];
            $item['value'] = $item['cri_code'];
            return $item;
        })->toArray();
        //获取设备列表
        $deviceModel = new DeviceModel();
        $where = [];
        $device_list = $deviceModel->where($where)->select()->toArray();
        $new_device_list = [];
        foreach ($device_list as $item){
            $new_item['cri_id'] = $item['serial_number'];
            $new_item['cri_code'] = $item['id'];
            $new_item['cri_name'] = $item['name'];
            $new_item['label'] = $item['name'];
            $new_item['value'] = $item['id'];
            $new_item['cri_lng'] = $item['lng'];
            $new_item['cri_lat'] = $item['lat'];
            $new_item['cri_superior_code'] = $item['address_id'];
            $new_device_list[] = $new_item;
        }
        $da = array_merge($da,$new_device_list);

        $list = $addressModel->getTree($da);


        return api_json_echo(true,'查询成功',$list);
    }
}
